package com.cnxjs.shs4et.controller;

import com.cnxjs.shs4et.common.Result;
import com.cnxjs.shs4et.entity.User;
import com.cnxjs.shs4et.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    // 用户注册
    @PostMapping("/register")
    public Result register(@RequestBody User user){
        return userService.register(user);
    }

    // 用户登录
    @PostMapping("/login")
    public Result login(@RequestBody User user){
        return userService.login(user);
    }

    // 用户信息修改
    @PutMapping("/update")
    public Result update(@RequestBody User user){
        return userService.update(user);
    }

    // 注销用户（伪删除）
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        return userService.delete(id);
    }

    // 分页排序查询用户
    @GetMapping("/")
    public Result findAll(@RequestParam(defaultValue= "0") Integer page,
                          @RequestParam(defaultValue= "20") Integer size,
                          @RequestParam(defaultValue = "publishTime") String sortRule){
        Sort sort = Sort.by(Sort.Direction.DESC, sortRule);
        Pageable pageable = PageRequest.of(page, size, sort);
        return userService.findAll(pageable);
    }

}
